#include "ServerTest.h"

ServerTest::ServerTest()
{
}
ServerTest::~ServerTest()
{
}
void ServerTest::Run(std::string ip,int port)
{
    moniter = std::make_shared<Listener>(ip,port);
    moniter->bind();
    while(1)
    {
        for(auto& v: userMgr)
        {
            v.second->Update();
        }
        std::set<uint32_t> v;
        moniter->EpollWait(v);
        for(auto id:v)
        {
            if(id == 0)
            {
                auto client = moniter->accept();
                userMgr.emplace(client->GetSeeeid(),client);
            }
            else
            {
                auto it = userMgr.find(id);
                if(it != userMgr.end())
                {
                    int len = 0;
                    do
                    {
                        char buf[1024];
                        len = it->second->Recv(buf,1024);
                        if(len > 0)
                            it->second->Send(buf,len);
                    }while(len > 0);
                }
            }
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}


